package com.zmn.oms.business.impl.conf.ordertag;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.conf.ordertag.ConfOrderTagCategoryBService;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.conf.ordertag.ConfOrderTagCategory;
import com.zmn.oms.model.entity.conf.ordertag.ConfOrderTagCategoryQuery;
import com.zmn.oms.model.entity.conf.ordertag.ConfOrderTagQuery;
import com.zmn.oms.model.vo.conf.ordertag.ConfOrderTagCategoryVO;
import com.zmn.oms.services.interfaces.conf.ordertag.ConfOrderTagCategoryService;
import com.zmn.oms.services.interfaces.conf.ordertag.ConfOrderTagService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：订单标签分类
 *
 * @author liuying
 * @date 2021/05/06 17:03
 */
@Slf4j
@Service
public class ConfOrderTagCategoryBServiceImpl implements ConfOrderTagCategoryBService {

    @Resource
    private ConfOrderTagCategoryService confOrderTagCategoryService;
    @Resource
    private ConfOrderTagService confOrderTagService;

    @Override
    public List<ConfOrderTagCategoryVO> listPageByQuery(ConfOrderTagCategoryQuery query) {
        if (StringUtils.isNotBlank(query.getKeyWord())) {
            if (NumberUtils.isDigits(query.getKeyWord())) {
                query.setId(Integer.valueOf(query.getKeyWord()));
            } else {
                query.setLikeName(query.getKeyWord());
            }
        }
        List<ConfOrderTagCategory> confList = confOrderTagCategoryService.listPageByQuery(query);
        return BeanMapper.mapList(confList, ConfOrderTagCategoryVO.class);
    }

    @Override
    public void modifyCategoryById(ConfOrderTagCategory category) throws OmsBaseException {
        Objects.requireNonNull(category.getCategId(), "ID不能为空");

//        ConfOrderTagCategoryQuery query = ConfOrderTagCategoryQuery.builder()
//                .notId(category.getCategId())
//                .categName(category.getCategName()).build();
//        int count = confOrderTagCategoryService.countByQuery(query);
//        if (count > 0) {
//            throw new OmsBaseException("名称已存在");
//        }

        ConfOrderTagCategory updateCategory = ConfOrderTagCategory.builder()
                .categId(category.getCategId())
                .status(category.getStatus())
                .sort(category.getSort())
                .categName(category.getCategName())
                .remark(category.getRemark())
                .updater(category.getUpdater()).build();
        confOrderTagCategoryService.updateCategoryById(updateCategory);
    }

    @Override
    public void modifyCategoryStatusById(ConfOrderTagCategory category) throws OmsBaseException {
        Objects.requireNonNull(category.getCategId(), "ID不能为空");
        if (Objects.equals(category.getStatus(), GlobalConsts.NO)) {
            ConfOrderTagQuery confOrderTagQuery = ConfOrderTagQuery.builder().categoryId(category.getCategId()).build();
            int count = confOrderTagService.countByQuery(confOrderTagQuery);
            if (count > 0) {
                throw new OmsBaseException("分类使用中，无法禁用");
            }
        }
        ConfOrderTagCategory updateCategory = ConfOrderTagCategory.builder()
                .categId(category.getCategId())
                .status(category.getStatus())
                .updater(category.getUpdater()).build();
        confOrderTagCategoryService.updateCategoryById(updateCategory);
    }

    @Override
    public void modifyCategorySortById(ConfOrderTagCategory category) {
        Objects.requireNonNull(category.getCategId(), "ID不能为空");

        ConfOrderTagCategory updateCategory = ConfOrderTagCategory.builder()
                .categId(category.getCategId())
                .sort(category.getSort())
                .updater(category.getUpdater()).build();
        confOrderTagCategoryService.updateCategoryById(updateCategory);
    }

    @Override
    @Transactional
    public Integer addCategory(ConfOrderTagCategory category) throws OmsBaseException {
        // 重复检查
//        ConfOrderTagCategoryQuery query = ConfOrderTagCategoryQuery.builder()
//                .categName(category.getCategName()).build();
//        int count = confOrderTagCategoryService.countByQuery(query);
//        if (count > 0) {
//            throw new OmsBaseException("名称已存在");
//        }

        return confOrderTagCategoryService.insertCategory(category);
    }
}
